package de.wesemann.mediahandbook.le.util;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.util.Log;
import de.wesemann.jdedun.entities.Paging;
import de.wesemann.jdedun.entities.mediahandbook.branches.MediahandbookBranche;
import de.wesemann.jdedun.entities.mediahandbook.branches.MediahandbookBranchesContainer;
import de.wesemann.jdedun.exceptions.PageNotFoundException;
import de.wesemann.jdedun.implementation.mediahandbook.MediahandbookBranchesReq;

public class BranchUtil {
	private static final MediahandbookBranchesReq BRANCHES_REQ = new MediahandbookBranchesReq();
	private static final String LOG_TAG = BranchUtil.class.getSimpleName();
	private static final HashMap<String, String> SEARCH_TUPLE = new HashMap<String, String>();

	public static MediahandbookBranchesContainer getFirstBranches(int limit) throws FileNotFoundException {
		Log.d(LOG_TAG, "Trying to get all branchs");

		return BRANCHES_REQ.getFirstN(limit);
	}

	public static MediahandbookBranchesContainer getNextBranchs(Paging<MediahandbookBranche> page)
			throws PageNotFoundException, FileNotFoundException {
		return BRANCHES_REQ.getNext(page);
	}

	public static List<MediahandbookBranche> getAllBranches() throws FileNotFoundException {
		return BRANCHES_REQ.getAll();
	}

	public static MediahandbookBranchesContainer searchBranchByNameWithLimit(int limit, String branchName)
			throws FileNotFoundException {
		SEARCH_TUPLE.clear();
		SEARCH_TUPLE.put("name", branchName);
		return BRANCHES_REQ.searchWithLimit(limit, SEARCH_TUPLE);
	}

	public static List<MediahandbookBranche> searchBranchById(int id) throws FileNotFoundException {
		Log.d(LOG_TAG, "looking for a branch with the id: " + id);
		SEARCH_TUPLE.clear();
		SEARCH_TUPLE.put("id", String.valueOf(id));
		List<MediahandbookBranche> branches = BRANCHES_REQ.search(SEARCH_TUPLE);
		Log.d(LOG_TAG, "found " + branches.size() + " branches with the Id " + id);
		SEARCH_TUPLE.clear();
		return branches;
	}

	public static List<MediahandbookBranche> searchBranchByParentId(int parentId) throws FileNotFoundException {
		Log.d(LOG_TAG, "looking for a branch with the parent_id: " + parentId);
		SEARCH_TUPLE.clear();
		SEARCH_TUPLE.put("parent_id", String.valueOf(parentId));
		List<MediahandbookBranche> branches = BRANCHES_REQ.search(SEARCH_TUPLE);
		Log.d(LOG_TAG, "found " + branches.size() + " branches with the parent Id " + parentId);
		SEARCH_TUPLE.clear();
		return branches;
	}

	public static List<MediahandbookBranche> searchBrnachesByName(String branchName) throws FileNotFoundException {
		SEARCH_TUPLE.clear();
		Log.d(LOG_TAG, "looking for a branch with the name: " + branchName);
		SEARCH_TUPLE.put("name", String.valueOf(branchName));
		List<MediahandbookBranche> branches = BRANCHES_REQ.search(SEARCH_TUPLE);
		Log.d(LOG_TAG, "found " + branches.size() + " branches with the name " + branchName);
		Log.d(LOG_TAG, "found " + branches.toString());
		SEARCH_TUPLE.clear();
		return branches;
	}

	public static List<MediahandbookBranche> searchBranchById(List<String> subBranches) throws FileNotFoundException {
		Log.d(LOG_TAG, "looking for a branch with the id: " + subBranches);
		List<MediahandbookBranche> branches = new ArrayList<MediahandbookBranche>();
		SEARCH_TUPLE.clear();
		for (String branchId : subBranches) {
			SEARCH_TUPLE.put("id", String.valueOf(branchId));
			branches.addAll(BRANCHES_REQ.search(SEARCH_TUPLE));
			Log.d(LOG_TAG, "found " + branches.size() + " branches with the Id " + subBranches);
			SEARCH_TUPLE.clear();
		}

		return branches;
	}

}
